from flask import request, Blueprint
from schema.cron_job import CronJobCreate, CronJobUpdate, CronJobDelete, CronJobFilter
from common.utils import json_response, exception_decorate
from eval_lib.common import logger
from service.cron_job import CronJobWorker

cron_app_app = Blueprint('cron_app_app', __name__)
log = logger.get_logger()


@cron_app_app.route("/cron-job", methods=["POST"])
@exception_decorate
def post_jobs():
    json_data = request.json
    cj = CronJobCreate(json_data)
    cj.is_valid()

    res, _ = CronJobWorker.Post(info=cj)
    return json_response(data=res), 200


@cron_app_app.route("/cron-job", methods=["PATCH"])
@exception_decorate
def update_jobs():
    json_data = request.json
    cj = CronJobUpdate(json_data)
    cj.is_valid()

    res, _ = CronJobWorker.Update(info=cj)
    return json_response(data=res), 200


@cron_app_app.route("/cron-job", methods=["DELETE"])
@exception_decorate
def delete_jobs():
    json_data = request.json
    cj = CronJobDelete(json_data)
    cj.is_valid()

    res, _ = CronJobWorker.Delete(info=cj)
    return json_response(data=res), 200


@cron_app_app.route("/cron-job", methods=["GET"])
@exception_decorate
def get_jobs():
    args = request.args
    cj = CronJobFilter(**args)

    res, page = CronJobWorker.Get(info=cj)
    return json_response(data=res, page=page.to_json()), 200
